package com.dianrui.firemenmonitor.activity;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.util.OkHttpUtils;
import com.dianrui.firemenmonitor.util.SharedUtil;
import com.dianrui.firemenmonitor.util.StringUtils;
import com.dianrui.firemenmonitor.util.ToastUtil;
import com.dianrui.firemenmonitor.util.Url;
import com.dianrui.firemenmonitor.view.CountDownButton;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 */

public class ForgetPasswordActivity extends BaseActivity {
    private ProgressDialog mProgressDialog;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.forget_mobile_ed)
    EditText forgetMobileEd;
    @BindView(R.id.forget_code_ed)
    EditText forgetCodeEd;
    @BindView(R.id.countdown)
    CountDownButton countdown;
    @BindView(R.id.forget_resetcode_ed)
    EditText forgetResetcodeEd;
    @BindView(R.id.forget_resetcodesure_ed)
    EditText forgetResetcodesureEd;
    @BindView(R.id.save_btn)
    Button saveBtn;


    @Override
    public int getContentViewId() {
        return R.layout.activity_forgetpwd;
    }

    @Override
    public void initView() {
        customInit(R.color.blue_518bfa);
        mProgressDialog = new ProgressDialog(this);
        title.setText(getString(R.string.forgetpwd_title));
        countdown.setOnOperationCallBack(new CountDownButton.CountDownButtonOperationCallBack() {
            @Override
            public void onFinish() {
                countdown.millisInFuture(60000);
                countdown.setText(getString(R.string.forgetpwd_getcode));
                countdown.setTextColor(getResources().getColor(R.color.blue_sky));
            }

            @Override
            public void onTick(long millisUntilFinished, CountDownButton countDownButton) {
                String tickTxts = millisUntilFinished / 1000 + "秒后再试";
                countdown.setText(tickTxts);
                countdown.setTextColor(getResources().getColor(R.color.blue_sky));
            }

            @Override
            public void onClickFromUser(boolean isRunning, long millisUntilFinished, CountDownButton countDownButton) {
                getVerificationCode();
            }
        });
        forgetMobileEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (forgetMobileEd.length() > 0 && forgetCodeEd.length() > 0 && forgetResetcodeEd.length() > 0 && forgetResetcodesureEd.length() > 0) {
                    saveBtn.setBackgroundResource(R.drawable.forgetpwd_blue_shape);
                } else {
                    saveBtn.setBackgroundResource(R.drawable.forgetpwd_gray_shape);
                }
            }
        });

        forgetCodeEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (forgetMobileEd.length() > 0 && forgetCodeEd.length() > 0 && forgetResetcodeEd.length() > 0 && forgetResetcodesureEd.length() > 0) {
                    saveBtn.setBackgroundResource(R.drawable.forgetpwd_blue_shape);
                } else {
                    saveBtn.setBackgroundResource(R.drawable.forgetpwd_gray_shape);
                }
            }
        });
        forgetResetcodeEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (forgetMobileEd.length() > 0 && forgetCodeEd.length() > 0 && forgetResetcodeEd.length() > 0 && forgetResetcodesureEd.length() > 0) {
                    saveBtn.setBackgroundResource(R.drawable.forgetpwd_blue_shape);
                } else {
                    saveBtn.setBackgroundResource(R.drawable.forgetpwd_gray_shape);
                }
            }
        });

        forgetResetcodesureEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (forgetMobileEd.length() > 0 && forgetCodeEd.length() > 0 && forgetResetcodeEd.length() > 0 && forgetResetcodesureEd.length() > 0) {
                    saveBtn.setBackgroundResource(R.drawable.forgetpwd_blue_shape);
                } else {
                    saveBtn.setBackgroundResource(R.drawable.forgetpwd_gray_shape);
                }
            }
        });


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null &&mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    private void getVerificationCode() {
        if (StringUtils.isEmpty(forgetMobileEd.getText().toString())) {
            ToastUtil.showToast("请输入你的手机号码");
            return;
        }
        if (forgetMobileEd.getText().length() < 11) {
            ToastUtil.showToast("手机号码必须是11位");
            return;
        }

        if (countdown.getmCurrentmillis() > 2000) {
            ToastUtil.showToast(countdown.getmCurrentmillis() / 1000 + "秒后再试");
            return;
        }
        mProgressDialog.setMessage("正在发送短信中...");
        mProgressDialog.show();
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("phone", forgetMobileEd.getText().toString().trim());//手机号
        OkHttpUtils.getInstance().postRequests(Url.SMS, jsonObject.toString(),  new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                mProgressDialog.dismiss();
                                countdown.start();
                                ToastUtil.showToast(jsonObject.optString("msg"));
                            } else {
                                mProgressDialog.dismiss();
                                countdown.cancel();
                                countdown.setText(getString(R.string.forgetpwd_getcode));
                                countdown.setTextColor(getResources().getColor(R.color.blue_sky));
                                ToastUtil.showToast(jsonObject.optString("msg"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                } else {
                    mProgressDialog.dismiss();
                    countdown.cancel();
                    countdown.setText(getString(R.string.forgetpwd_getcode));
                    countdown.setTextColor(getResources().getColor(R.color.blue_sky));
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }


    @OnClick({R.id.back, R.id.save_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.save_btn:
                if (StringUtils.isEmpty(forgetMobileEd.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.forgetpwd_mobile_hint));
                    return;
                }
                if (StringUtils.isEmpty(forgetCodeEd.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.forgetpwd_code_hint));
                    return;
                }
                if (StringUtils.isEmpty(forgetResetcodeEd.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.forgetpwd_reset_hint));
                    return;
                }
                if (StringUtils.isEmpty(forgetResetcodesureEd.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.forgetpwd_reset_hint1));
                    return;
                }
                String psw1 = forgetResetcodeEd.getText().toString().trim();//重置密码
                String psw2 = forgetResetcodesureEd.getText().toString().trim();//确认密码
                if (!psw1.equals(psw2)) {
                    ToastUtil.showToast(getString(R.string.two_passwords_are_inconsistent));
                    return;
                }
                save();
                break;

            default:
                break;
        }
    }


    private void save() {
        mProgressDialog.setMessage(getString(R.string.submiting));
        mProgressDialog.show();
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("phone",forgetMobileEd.getText().toString().trim());
        jsonObject.addProperty("code",forgetCodeEd.getText().toString().trim());
        jsonObject.addProperty("password",forgetResetcodeEd.getText().toString().trim());
        jsonObject.addProperty("againpass",forgetResetcodesureEd.getText().toString().trim());
        OkHttpUtils.getInstance().postRequests(Url.FORGETPWD, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                mProgressDialog.dismiss();
                                ToastUtil.showToast(jsonObject.optString("msg"));
                                finish();
                            }else {
                                mProgressDialog.dismiss();
                                ToastUtil.showToast(jsonObject.optString("msg"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    mProgressDialog.dismiss();
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }
}
